package com.zhongan.tb;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import com.zhongan.tb.utils.FileUtils;

/**
 * �Ž�������
 *
 * @author za-zhubin002
 */
public class TBHelper {

    private static TBHelper instance = null;

    /**
     * ���ڳ�Ա�б�
     */
    private List<TBMember> teamgroup = null;

    /**
     * �Ž��ܷ���
     */
    private double sumFee = 200;

    /**
     * �Ƿ���Ҫ��̯����-true����Ҫ��false:����Ҫ
     */
    private boolean feeLock = true;

    private TBHelper() {
    }

    public static TBHelper getInstance() {
        if (null == instance) {
            instance = new TBHelper();
            instance.readMemberFromSrc();
        }
        return instance;
    }

    /**
     * ����
     */
    public void reset() {
        instance.readMemberFromSrc();
        feeLock = true;
    }

    /**
     * ������Ż�ȡ�ݽ�������
     *
     * @param orderNum
     * @return
     */
    public String getNameByOrderNumber(int orderNum) {
        String name = "";
        for (TBMember tbMember : teamgroup) {
            if (tbMember.getOrderNumber() == orderNum) {
                name = tbMember.getName();
                break;
            }
        }
        return name;

    }

    /**
     * ���ҳ�Ա����˳������Ȩ��ѡ�ֿ��Բ�����˳����ң���
     *
     * @param id ��ָ������˳���ID
     *           id = -1:�����˴��ң������ƴ���
     *           id > 0 :ѡ��Ҫ����ң�û�����Ҫ�����2��
     */
    public void riffle(int id) {
        List<TBMember> needSortList = new ArrayList<TBMember>();    //��Ҫ����˳���ѡ���б�
        //��ȡ��Ҫ�����ѡ��
        for (TBMember member : teamgroup) {
            //ѡ�ַ������������
            if (member.getId() == id) {
                //
                if (member.getSortTime() > 1)
                    return;
                else {
                    member.setSortTime(member.getSortTime() + 1);        //�������+1
                    member.setRemark("�����ǳ�" + member.getSortTime() + "��");
                    member.setFee(member.getFee() + 5.00);                //���η������Ϊ5Ԫ
                }
            }
            //�ж�ѡ��������Ȩ������Ȩ��������
            if (member.getNeedSort() == 1)
                needSortList.add(member);
        }
        Collections.shuffle(needSortList);
        for (int i = 0; i < needSortList.size(); i++) {
            needSortList.get(i).setOrderNumber(i + 1);
        }
    }

    /**
     * �������
     *
     * @param feegroup ���븶�ѵĳ�Ա�б�
     */
    public void calulateFee(List<Integer> feegroup) {
        //����ֻ�ܷ�̯һ��
        if (!feeLock)
            return;
        feeLock = false;
        //�ѷ�̯����
        double payedFee = 0.00;
        //ʣ���֧������
        double leftFee = 0.00;
        //��ȡ��Ҫ���ѵĳ�Ա��������֧�������ܺ�
        List<TBMember> needPayList = new ArrayList<>();
        for (TBMember member : teamgroup) {
            if (member.getFee() > 0)
                payedFee += member.getFee();
            for (Integer memberid : feegroup) {
                if (memberid == member.getId())
                    needPayList.add(member);
            }
        }
        leftFee = sumFee - payedFee;
        //���÷�̯
        int totalNPayCount = needPayList.size();
        for (int i = 0; i < totalNPayCount; i++) {
            double monkey = getRandomMonkey(totalNPayCount - i, leftFee);
            leftFee -= monkey;
            needPayList.get(i).setFee(needPayList.get(i).getFee() + monkey);
        }
    }

    /**
     * ���ļ��ж�ȡ�Ŷӳ�Ա�б�
     */
    public void readMemberFromSrc() {
        teamgroup = new ArrayList<TBMember>();
        List<String> strList = FileUtils.readByLineToList("data/team.txt");
        for (String str : strList) {
            //������##��ͷ���У�����ע��
            if (str.startsWith("##"))
                continue;
            if (str.startsWith("��")) {
                sumFee = Double.valueOf(str.substring(1));
                continue;
            }
            TBMember member = new TBMember();
            member.convertStrToMember(str, "\\|");
            teamgroup.add(member);
        }
    }

    public List<TBMember> getTeamgroup() {
        return teamgroup;
    }

    public void setTeamgroup(List<TBMember> teamgroup) {
        this.teamgroup = teamgroup;
    }

    /**
     * �������������
     *
     * @param remainSize  ʣ��������
     * @param remainMoney ʣ�������
     * @return
     */
    public double getRandomMonkey(int remainSize, double remainMoney) {
        if (remainSize == 1)
            return remainMoney;
        Random random = new Random();
        double min = 0.01;
        double max = remainMoney / remainSize * 2;
        double monkey = random.nextDouble() * max;
        monkey = monkey <= min ? min : monkey;
        monkey = Math.floor(monkey * 100) / 100;
        return monkey;
    }
}
